package com.itheima.controller;

import com.itheima.pojp.Dept;
import com.itheima.pojp.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
    * 查询部门
    */
    @RequestMapping("/list")
    public Result list(){
        log.info("部门查询");
        List<Dept> depts = deptService.list();
        return Result.success(depts);
    }
    /**
    * 删除部门
     * @RequestParam
    */
    @DeleteMapping
    public Result delete(Integer id){
        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping
    public Result save (@RequestBody Dept dept){
        log.info("新增部门，dept:{}",dept);
        deptService.save(dept);
        return Result.success();
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询，id:{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    /**
     * 修改部门
     * @param dept
     * @return
     */
    @GetMapping("/{id}")
    public Result update(@RequestBody Dept dept){
        log.info("修改部门，dept:{}",     dept);
        deptService.update(dept);
        return Result.success();
    }
}